package com.manage.project.biz.controller;

import java.io.IOException;
import java.util.List;

import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.CivilEnginneeringFacility;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.ICivilEnginneeringFacilityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 土建结构设施管理Controller
 *
 * @author 小赵
 * @date 2022-04-28
 */
@RestController
@RequestMapping("/civil/facility")
public class CivilEnginneeringFacilityController extends BaseController
{
    @Autowired
    private ICivilEnginneeringFacilityService civilEnginneeringFacilityService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;



    @PreAuthorize("@ss.hasPermi('civil:facility:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringFacility civilEnginneeringFacility)
    {
        startPage();
        List<CivilEnginneeringFacility> list = civilEnginneeringFacilityService.selectCivilEnginneeringFacilityList(civilEnginneeringFacility);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('civil:facility:export')")
    @Log(title = "土建结构设施管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export( CivilEnginneeringFacility civilEnginneeringFacility) throws IOException {
        List<CivilEnginneeringFacility> list = civilEnginneeringFacilityService.selectCivilEnginneeringFacilityList(civilEnginneeringFacility);
        ExcelUtil<CivilEnginneeringFacility> util = new ExcelUtil<CivilEnginneeringFacility>(CivilEnginneeringFacility.class);
        return util.exportExcel( list,"土建结构设施管理数据");
    }


    @PreAuthorize("@ss.hasPermi('civil:facility:query')")
    @GetMapping(value = "/{facilityId}")
    public AjaxResult getInfo(@PathVariable("facilityId") Long facilityId)
    {
        return AjaxResult.success(civilEnginneeringFacilityService.selectCivilEnginneeringFacilityByFacilityId(facilityId));
    }


    @PreAuthorize("@ss.hasPermi('civil:facility:add')")
    @Log(title = "土建结构设施管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringFacility civilEnginneeringFacility)
    {
        String roadName=civilEnginneeringFacility.getRoadName();
        String tunnelName=civilEnginneeringFacility.getTunnelName();
        String tunnelId=civilEnginneeringFacility.getTunnelId();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        if (civilEnginneeringFacility.getFacilityName()==null){
            return new AjaxResult().error("请输入设施名称");
        }
        return toAjax(civilEnginneeringFacilityService.insertCivilEnginneeringFacility(civilEnginneeringFacility));
    }


    @PreAuthorize("@ss.hasPermi('civil:facility:edit')")
    @Log(title = "土建结构设施管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringFacility civilEnginneeringFacility)
    {
        String roadName=civilEnginneeringFacility.getRoadName();
        String tunnelName=civilEnginneeringFacility.getTunnelName();
        String tunnelId=civilEnginneeringFacility.getTunnelId();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        if (civilEnginneeringFacility.getFacilityName()==null){
            return new AjaxResult().error("请输入设施名称");
        }
        return toAjax(civilEnginneeringFacilityService.updateCivilEnginneeringFacility(civilEnginneeringFacility));
    }


    @PreAuthorize("@ss.hasPermi('civil:facility:remove')")
    @Log(title = "土建结构设施管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{facilityIds}")
    public AjaxResult remove(@PathVariable Long[] facilityIds)
    {
        return toAjax(civilEnginneeringFacilityService.deleteCivilEnginneeringFacilityByFacilityIds(facilityIds));
    }
}
